package org.lmind.brick.service.web;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.lmind.brick.lib.UncaughtException;

/**
 * 可以方便的在url后添加参数
 * @author 刘勉
 *
 */
public class UrlParameters {
	
	private StringBuilder stringBuilder = new StringBuilder();
	
	private boolean firstAppend = true;
	
	/**
	 * 追加一个参数
	 * @param name 参数名
	 * @param value 参数值
	 * @throws UnsupportedEncodingException
	 */
	public void appendParameter(String name, Object value) {
		try {
			if (!firstAppend) {
				stringBuilder.append("&");
			} else {
				firstAppend = false;
			}
			stringBuilder.append(URLEncoder.encode(name, "utf-8"));
			stringBuilder.append("=");
			if (value != null)
				stringBuilder.append(URLEncoder.encode(value.toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			throw new UncaughtException(e);
		}
	}

	@Override
	public String toString() {
		return stringBuilder.toString();
	}
	
}
